package com.luke.source.pattern;

import io.netty.channel.ChannelFuture;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import java.util.ArrayList;
import java.util.List;

/**
 * 观察者模式
 *
 * 1、观察者
 * 2、被观察者
 * 3、被观察者事件触发回调观察者
 *
 */
public class ObserverTest {

    //被观察者
    public interface Observerable {
        void registerObserver(Observer observer);
        void removeObserver(Observer observer);
        void notifyObserver();
    }

    //观察者
    public interface Observer {
        void notify(String message);
    }

    //女神：被观察者
    public static class Girl implements Observerable {

        private String message;

        private List<Observer> observerList;

        public Girl() {
            this.observerList = new ArrayList<>();
        }

        @Override
        public void registerObserver(Observer observer) {
            observerList.add(observer);
        }

        @Override
        public void removeObserver(Observer observer) {
            observerList.remove(observer);
        }

        @Override
        public void notifyObserver() {
            for(Observer observer : observerList){
                observer.notify(message);
            }
        }

        //==============================================

        public void hasBoyFriend(){
            message = "女神有男朋友了";
            notifyObserver();
        }

        public void getMarried(){
            message = "女神结婚了";
            notifyObserver();
        }

        public void getSingled(){
            message = "女神恢复单身了";
            notifyObserver();
        }

    }

    //男孩、男人、老男人：观察者
    public static class Boy implements Observer{
        @Override
        public void notify(String message) {
            System.out.println("男孩收到消息："+message);
        }
    }
    public static class Man implements Observer{
        @Override
        public void notify(String message) {
            System.out.println("男人收到消息："+message);
        }
    }
    public static class OldMan implements Observer{
        @Override
        public void notify(String message) {
            System.out.println("老男人收到消息："+message);
        }
    }

    public static void main(String[] args) {
        //被观察者
        Girl girl = new Girl();
        //观察者
        Boy boy = new Boy();
        Man man = new Man();
        OldMan oldMan = new OldMan();

        //开始观察
        girl.registerObserver(boy);
        girl.registerObserver(man);
        girl.registerObserver(oldMan);

        //事件触发
        girl.hasBoyFriend();
        System.out.println("=====================");
        girl.removeObserver(oldMan);
        girl.getMarried();
        System.out.println("=====================");
        girl.registerObserver(oldMan);
        girl.getSingled();

    }

    //Netty中的观察者模式（Promise模式）
    public void nettyPromise(NioSocketChannel channel,Object object){
        //返回观察者ChannelFuture（ChannelPromise）
        ChannelFuture channelFuture = channel.writeAndFlush(object);

        //对channelFuture添加被观察者
        channelFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                //通知回调（由观察者负责调用）
            }
        });

        //添加第二个观察者
        channelFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
            }
        });

    }

}
